home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15815 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: ub239.dialup.uwa.edu.au!localhost!prye
  2. From: prye@cyllene.uwa.edu.au (Peter Rye)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: returning an array from function
  5. Date: 08 Apr 1996 06:49:15 GMT
  6. Organization: The University of Western Australia
  7. Message-ID: <PRYE.96Apr8144915@cyllene.uwa.edu.au>
  8. References: <4jstd8$kh0@news1.sunbelt.net>
  9.     <Pine.A32.3.91.960407035313.115982E-100000@magritte.its.rpi.edu>
  10. NNTP-Posting-Host: ub239.dialup.uwa.edu.au
  11. In-reply-to: Kengo Hashimoto's message of Sun, 7 Apr 1996 03:56:27 -0400
  12. X-Mailer: GNU Emacs 19.28
  13.  
  14. >>>>> "Kengo" == Kengo Hashimoto <hashik@rpi.edu> writes:
  15.  
  16.     Kengo> Just do a call-by-reference to the array, or return a
  17.     Kengo> pointer to the first member of the array. Here are
  18.     Kengo> examples:
  19.  
  20.     Kengo> void foo(int &[]); // just pass the array in there,
  21.   
  22. This declares the argument as an array of references to int.
  23. I believe this is not legal.
  24.   
  25.     Kengo> manipulate it // within foo(), and the original copy would
  26.     Kengo> be // modified as well.
  27.  
  28. An array argument to a function decays to a pointer to the first element
  29. of the array. A reference is not needed to modify the original copy.
  30.  
  31.     Kengo> returning a pointer: int *foo();
  32.  
  33.     Kengo> int bar[arraysize];
  34.  
  35.     Kengo> bar = foo();
  36.  
  37. You can't do this.
  38. An array name is not an l-value. (ie: can't be assigned to).
  39.  
  40.     Kengo> foo() { foo2[arraysize]; return &(foo2[0]); }
  41.  
  42. This is very naughty.
  43. foo2[] is local to the function foo.
  44. ie: it will not exist when the function returns (at least you shouldn't
  45. assume that it will exist.)
  46.  
  47.  
  48. --
  49. Peter Rye   prye@cyllene.uwa.edu.au,  prye@ichr.uwa.edu.au
  50. Respiratory Research Fellow, Princess Margaret Hospital for Children
  51. Perth, Western Australia    Ph: +61 (09) 340 8985, Fax: +61 (09) 388 2097
  52. ** Smoking areas in restaurants are like peeing areas in swimming pools. **
  53.